package com.sichem.domain.activity.service.discount.impl;

import com.sichem.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.sichem.domain.activity.service.discount.AbstractDiscountCalculateService;
import com.sichem.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Author xiangshizhen
 * @Date 2025/2/11
 * 满减
 */

@Service("MJ")
@Slf4j
public class MJCalculateService extends AbstractDiscountCalculateService {


    @Override
    protected BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        // 获取满减规则
        String marketExpr = groupBuyDiscount.getMarketExpr();
        String[] split = marketExpr.split(Constants.SPLIT);
        BigDecimal x = new BigDecimal(split[0]);
        BigDecimal y = new BigDecimal(split[1]);

        // 小于满减起步金额 返回原始价格
        if (originalPrice.compareTo(x) < 0) {
            return originalPrice;
        }
        // 计算满减后的金额
        BigDecimal deductionPrice = originalPrice.subtract(y);
        // 小于0元 返回0.01元
        deductionPrice = limit(deductionPrice);

        return deductionPrice;
    }
}
